package com.goods.dao.impl;

import java.util.Date;

import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.common.domain.GoodsVo;
import com.common.param.GoodsParam;
import com.common.query.QueryResult;
import com.common.resource.GoodsResource;
import com.common.tool.DateUtil;
import com.common.tool.StringUtils;
import com.goods.dao.SearchGoodsWithLuceneDao;
import com.goods.lucene.tool.FclubBooleanQuery;
import com.goods.lucene.tool.IndexManage;

/**
 * 
 * @author penghui.li
 *
 */
@Repository("searchGoodsWithLuceneDao")
public class SearchGoodsWithLuceneDaoImpl  implements  SearchGoodsWithLuceneDao{
	private Logger logger = LoggerFactory.getLogger(this.getClass().getName());
	/**
	 * 通过lucene查询商品信息
	 * @param param
	 * @return
	 * @throws Throwable 
	 */
	@SuppressWarnings({ "unused" })
	public QueryResult<GoodsVo> queryGoodsWithLucene(GoodsParam param) throws Throwable{
		// TODO Auto-generated method stub
		if (param == null) {
			logger.info("queryGoodsWithLucene：参数为空");
		}
		BooleanQuery booleanMainQuery = new BooleanQuery();
		GoodsVo vo = param.getEntity();
		QueryResult<GoodsVo> queryResult = new QueryResult<GoodsVo>();
		try {
			if(null != vo){
				BooleanQuery booleanCondition = new BooleanQuery();
				//商品名称
				if(StringUtils.isNotNULL(vo.getGoodsName())){
					String queryStr = GoodsResource.FIELD_GOODS_NAME + ":" + vo.getGoodsName();
					booleanCondition.add(FclubBooleanQuery.getBooleanQuery(queryStr,
							GoodsResource.FIELD_GOODS_NAME), BooleanClause.Occur.MUST);
				}
				//商品名称
				if(StringUtils.isNotNULL(vo.getBrandName())){
					String queryStr = GoodsResource.FIELD_BRAND_NAME + ":" + vo.getBrandName();
					booleanCondition.add(FclubBooleanQuery.getBooleanQuery(queryStr,
							GoodsResource.FIELD_BRAND_NAME), BooleanClause.Occur.MUST);
				}
				//性别
				if(StringUtils.isNotNULL(vo.getGoodsSexCn())){
					String queryStr = GoodsResource.FIELD_GOODS_SEX_CN + ":" + vo.getGoodsSexCn();
					booleanCondition.add(FclubBooleanQuery.getBooleanQuery(queryStr,
							GoodsResource.FIELD_GOODS_SEX_CN), BooleanClause.Occur.MUST);
				}
//				int a = booleanCondition.getMaxClauseCount();
//				if (a > 0) {
//					booleanMainQuery.add(booleanCondition, BooleanClause.Occur.MUST);
//				} 
			}else{
				logger.info("queryGoodsWithLucene：参数为空");
			}
			if (StringUtils.isNotNull(param.getStartTm()) && StringUtils.isNotNull(param.getEndTm())) {
				Query query = NumericRangeQuery.newLongRange(GoodsResource.FIELD_CREATE_TIME, DateUtil.parseDateString(param.getStartTm(), "yyyy-MM-dd hh:mm:ss")
						.getTime(), DateUtil.parseDateString(param.getEndTm(),"yyyy-MM-dd hh:mm:ss").getTime(), true, true);
				booleanMainQuery.add(query, BooleanClause.Occur.MUST);
			}
			if (booleanMainQuery == null)
				throw new Exception("queryGoodsWithLucene：参数错误！");
			System.out.println("*********************查询条件："+booleanMainQuery);
			logger.info("*********************查询条件："+booleanMainQuery);
			queryResult = IndexManage.search(booleanMainQuery,param.getStart(),param.getOffset()+param.getStart());
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		return queryResult;
	}
	
	public static void main(String[] args) {
		Date date =  DateUtil.parseDateString("2012-05-06 12:1:1", "yyyy-MM-dd hh:mm:ss");
		System.out.println(date.getTime());
	}
	
}
